package com.bdqn.tools;

import java.util.List;

/**
 * Page 通用分页对象
 *
 * @author LILIBO
 * @since 2024/4/25
 */
public class Page<T> {

    // 当前页码（当前是第几页）
    private Integer currentPageNo;

    // 每页大小（每页显示多少条数据）
    private Integer pageSize = 2;

    // 总数据量（数据库中满足条件的数据一共有多少）
    private Integer totalCount;

    // 总页数（通过总数据量和页大小计算出来）
    private Integer totalPageCount;

    // 分页数据
    private List<T> data; // ${page.data[0].subjectName}

    public Integer getCurrentPageNo() {
        return currentPageNo;
    }

    public void setCurrentPageNo(Integer currentPageNo) {
        // 容错处理，如果小于等于0就设置为1，不能超过最大页大小
        this.currentPageNo = currentPageNo <= 0 ? 1 : Math.min(currentPageNo, this.totalPageCount == 0 ? 1 : this.totalPageCount);
    }

    /**
     * 获取当时页的起始下标
     *
     * @return
     */
    public Integer getPageIndex(){
        return (this.currentPageNo - 1) * this.pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    /**
     * 设置总数据量
     *
     * @param totalCount
     */
    public void setTotalCount(Integer totalCount) {
        // 设置总数据量
        this.totalCount = totalCount; // 20
        // 设置总页数（根据pageSize进行计算）
        this.totalPageCount = (this.totalCount % this.pageSize == 0) ? (this.totalCount / this.pageSize) : (this.totalCount / this.pageSize + 1);

        // 等效于
        /* if (this.totalCount % pageSize == 0) {
            this.totalPage = this.totalCount / this.pageSize;
        } else {
            this.totalPage = this.totalCount / this.pageSize + 1;
        } */
    }

    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public void setTotalPageCount(Integer totalPageCount) {
           this.totalPageCount = totalPageCount;
        // 等效于
         if (this.totalCount % pageSize == 0) {
            this.totalPageCount = this.totalCount / this.pageSize;
        } else {
            this.totalPageCount = this.totalCount / this.pageSize + 1;
        }
    }
}
